package com.ruoyi.project.channel.salesmanInfo.controller;

import java.math.BigDecimal;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.channel.salesmanInfo.domain.SalesmanInfo;
import com.ruoyi.project.channel.salesmanInfo.service.ISalesmanInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 业务人员信息操作处理
 * 
 * @author CAIN
 * @date 2024-10-21
 */
@Controller
@RequestMapping("/channel/salesmanInfo")
public class SalesmanInfoController extends BaseController
{
    private String prefix = "channel/salesmanInfo";
	
	@Autowired
	private ISalesmanInfoService salesmanInfoService;
	
	@RequiresPermissions("channel:salesmanInfo:view")
	@GetMapping()
	public String salesmanInfo()
	{
	    return prefix + "/salesmanInfo";
	}
	
	/**
	 * 查询业务人员列表
	 */
	@RequiresPermissions("channel:salesmanInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SalesmanInfo salesmanInfo)
	{
		startPage();
        List<SalesmanInfo> list = salesmanInfoService.selectSalesmanInfoList(salesmanInfo);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出业务人员列表
	 */
	@RequiresPermissions("channel:salesmanInfo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SalesmanInfo salesmanInfo)
    {
    	List<SalesmanInfo> list = salesmanInfoService.selectSalesmanInfoList(salesmanInfo);
        ExcelUtil<SalesmanInfo> util = new ExcelUtil<SalesmanInfo>(SalesmanInfo.class);
        return util.exportExcel(list, "salesmanInfo");
    }
	
	/**
	 * 新增业务人员
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存业务人员
	 */
	@RequiresPermissions("channel:salesmanInfo:add")
	@Log(title = "业务人员", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(SalesmanInfo salesmanInfo)
	{		
		return toAjax(salesmanInfoService.insertSalesmanInfo(salesmanInfo));
	}

	/**
	 * 修改业务人员
	 */
	@GetMapping("/edit/{userId}")
	public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
	{
		SalesmanInfo salesmanInfo = salesmanInfoService.selectSalesmanInfoById(userId);
		mmap.put("salesmanInfo", salesmanInfo);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存业务人员
	 */
	@RequiresPermissions("channel:salesmanInfo:edit")
	@Log(title = "业务人员", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(SalesmanInfo salesmanInfo)
	{		
		return toAjax(salesmanInfoService.updateSalesmanInfo(salesmanInfo));
	}

	@RequiresPermissions("system:user:edit")
	@Log(title = "业务人员", businessType = BusinessType.UPDATE)
	@PostMapping("/addOrUpdate")
	@ResponseBody
	public AjaxResult addOrUpdate(SalesmanInfo salesmanInfo)
	{
		SalesmanInfo salesmanInfo1 = salesmanInfoService.selectSalesmanInfoById(salesmanInfo.getUserId());
		if(salesmanInfo1 == null) {
			return toAjax(salesmanInfoService.insertSalesmanInfo(salesmanInfo));
		}
		return toAjax(salesmanInfoService.updateSalesmanInfo(salesmanInfo));
	}

	@RequiresPermissions("system:user:edit")
	@Log(title = "业务人员", businessType = BusinessType.UPDATE)
	@PostMapping("/updateAddress")
	@ResponseBody
	public AjaxResult updateAddress(SalesmanInfo salesmanInfo)
	{
		SalesmanInfo salesmanInfo1 = salesmanInfoService.selectSalesmanInfoById(salesmanInfo.getUserId());
		if(salesmanInfo1 == null) {
			return AjaxResult.error("请先设置汇率");
		}
		return toAjax(salesmanInfoService.updateSalesmanInfo(salesmanInfo));
	}

	@Log(title = "业务人员", businessType = BusinessType.UPDATE)
	@GetMapping("/getUser/{userId}")
	@ResponseBody
	public AjaxResult getUser(@PathVariable Long userId)
	{
		SalesmanInfo salesmanInfo = salesmanInfoService.selectSalesmanInfoById(userId);
		if(salesmanInfo == null) {
			salesmanInfo = new SalesmanInfo();
			salesmanInfo.setRate(BigDecimal.ZERO);
		}
		return AjaxResult.success("success",salesmanInfo);
	}

	/**
	 * 删除业务人员
	 */
	@RequiresPermissions("channel:salesmanInfo:remove")
	@Log(title = "业务人员", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(salesmanInfoService.deleteSalesmanInfoByIds(ids));
	}
	
}
